package org.apache.shiro.web.validator;

import com.dhcc.DRGsHosp.infra.util.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.servlet.http.HttpServletRequest;

/**
 * @title		:JiaHeLoginValidator.java
 * @description	:嘉和单点登录验证
 * @author		:WangTeng
 * @date		:2019年1月21日
 * @version     :1.0
 * @copyright   :DHCC 2013-2019
 */
public class JiaHeLoginValidator extends LoginValidator{

	private String sessionID;//登录用户的唯一标识

	private String username;

	private String ticketValidateUrl;//portal 单点登录验证票据接口地址

	private String ticketValidateMethod;//portal 单点登录验证票据接口方法

	public JiaHeLoginValidator() {

	}
	/**
	 * @desc Portal系统单点登录验证
	 * @param httpRequest
	 * @param ticketValidateUrl
	 * @param ticketValidateMethod
	 */
	public JiaHeLoginValidator(HttpServletRequest httpRequest, String ticketValidateUrl, String ticketValidateMethod) {
		//Portal单点登录验证
		this.sessionID = httpRequest.getParameter("sessionID");
		this.ticketValidateUrl = ticketValidateUrl;
		this.ticketValidateMethod = ticketValidateMethod;
	}
	
	@Override
	public boolean hasRequiredParam(HttpServletRequest httpRequest) {
		this.sessionID = httpRequest.getParameter("sessionID");
		if (this.sessionID != null) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isValidate() {
		if (StringUtils.isEmpty(sessionID)) {
			return false;
		} else {
			JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
			Client client = factory.createClient(ticketValidateUrl);
			try {
			    String param = "<REQUEST JHIPMsgVersion=\"1.0.1\"><JHSESSIONID>"+sessionID+"</JHSESSIONID></REQUEST>";

				Object[] objects = client.invoke(ticketValidateMethod, param);
				String xmlStr = objects[0]+"";
				System.out.println("JiaHe return: " + xmlStr);

                Document document = DocumentHelper.parseText(xmlStr);
                Element response = document.getRootElement();
                Element content = response.element("RESULT_CONTENT");

                String userCode = content.element("USER_CODE").getText();//用户账号
                System.out.println("username: " + userCode);
                if (userCode != null && StringUtils.hasLength(userCode)) {
					username= userCode;
					System.out.println("嘉和单点登录");
					return true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
		
	}

	/* (non-Javadoc)
	 * @see org.apache.shiro.web.filter.LoginValidator#getUsername()
	 */
	@Override
	public String getUsername() {
		return username;
	}

	/**
	 * @return the ticketValidateUrl
	 */
	public String getTicketValidateUrl() {
		return ticketValidateUrl;
	}

	/**
	 * @param ticketValidateUrl the ticketValidateUrl to set
	 */
	public void setTicketValidateUrl(String ticketValidateUrl) {
		this.ticketValidateUrl = ticketValidateUrl;
	}

	/**
	 * @return the ticketValidateMethod
	 */
	public String getTicketValidateMethod() {
		return ticketValidateMethod;
	}

	/**
	 * @param ticketValidateMethod the ticketValidateMethod to set
	 */
	public void setTicketValidateMethod(String ticketValidateMethod) {
		this.ticketValidateMethod = ticketValidateMethod;
	}

	public static void main(String[] args){
		String xml = "<RESPONSE JHIPMsgVersion=\"1.0.1\">\n" +
				"<RESULT_SYSTEM_NO> true </RESULT_SYSTEM_NO>\n" +
				"<RESULT_CONTENT>\n" +
				" <USER_CODE>00000564</USER_CODE>\n" +
				"<USER_NAME>张建斌</USER_NAME>\n" +
				"<SEX>1</SEX>\n" +
				"<BIRTHDAY>1972-01-02 00:00:00</BIRTHDAY>\n" +
				"<WORK_PHONE>010-3333-3443</WORK_PHONE>\n" +
				"<USER_LOGIN_NAME>null</USER_LOGIN_NAME>\n" +
				"<CREATE_DATE>null</CREATE_DATE>\n" +
				"<PASSWORD>null</PASSWORD>\n" +
				"<DEPT_CODE>0104040102</DEPT_CODE>\n" +
				"<DEPT_NAME>泌尿外科病区</DEPT_NAME>\n" +
				"<CA_CODE>null</CA_CODE>\n" +
				"<CA_BDDATE>null</CA_BDDATE>\n" +
				"<CA_STATUS>0</CA_STATUS>" +
				"<ID_CODE>612128197201020016</ID_CODE>\n" +
				"<PYM>ZJB</PYM>\n" +
				"<IS_LOGIN>0</IS_LOGIN>\n" +
				"<LOGIN>0</LOGIN>\n" +
				"<FIRSTJOB_DAY>null</FIRSTJOB_DAY>\n" +
				"<PWD_MODIFY_TIME>null</PWD_MODIFY_TIME>\n" +
				"<IS_STOP>0</IS_STOP>\n" +
				"</RESULT_CONTENT>\n" +
				"</RESPONSE>";

		try {
			Document document = DocumentHelper.parseText(xml);
			Element element = document.getRootElement();

			Element content = element.element("RESULT_CONTENT");
			System.out.println(content.element("USER_NAME").getText());
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	
}
